﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace thinger.AutomaticStoreMotionPro
{
    public partial class FrmCode : Form
    {
        #region 构造函数
        public FrmCode()
        {
            InitializeComponent();
            this.Load += FrmConfirm_Load;
        }

        // : this() 的意思是指，表示重载该类的无参数构造函数
        public FrmCode(string title, string msg, string oKMsg, string cancelMsg) : this()
        {
            this.lblTitle.Text = title;
            this.lblMessage.Text = msg;
            this.btnOK.Text = oKMsg;
            this.btnCancel.Text = cancelMsg;
        }
        #endregion

        #region 属性和对象
        public string code;
        #endregion

        #region 窗体加载
        private void FrmConfirm_Load(object sender, EventArgs e)
        {
            SetToolTip();
        }

        private void SetToolTip()
        {
            //定义ToolTip对象
            ToolTip toolTip = new ToolTip();
            toolTip.AutoPopDelay = 5000;
            toolTip.InitialDelay = 100;
            toolTip.ReshowDelay = 500;
            toolTip.ShowAlways = true;

        }
        #endregion

        //*************界面操作相关事件触发**************
        #region 无边框拖动

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void TopPanel_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        #endregion

        #region 窗体重绘事件 绘制一条边线
        private void FrmConfirm_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pen = new Pen(this.panel1.BackColor);
            Rectangle rectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            g.DrawRectangle(pen, rectangle);
        }
        #endregion


        #region 按钮操作事件
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.txtCode.Text.Length == 0)
            {
                new FrmConfirmSingleButton("条形码错误", "请扫码或输入正确的条形码!").ShowDialog();
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.code = this.txtCode.Text;
            this.Close();
        }

        private void txtCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOK_Click(null, null);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region 条码数据赋值
        public void ScannerRec(string message)
        {
            this.txtCode.Text = message;
        }
        #endregion


    }
}
